package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.domain.UserDomain;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/17.
 */

public class ModifyPayPassActivity extends AppCompatActivity {
    public Dialog loadingdialog = null;
    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.inputpass_iv)
    ImageView inputpassIv;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.inputpass_rl)
    RelativeLayout inputpassRl;
    @BindView(R.id.inputpassconfirm_iv)
    ImageView inputpassconfirmIv;
    @BindView(R.id.edit_passwordconfirm)
    EditText editPasswordconfirm;
    @BindView(R.id.inputpassconfirm_rl)
    RelativeLayout inputpassconfirmRl;
    @BindView(R.id.modifybt)
    RelativeLayout modifybt;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Tools.initStatus(this, R.color.black);
        setContentView(R.layout.activity_paypass);
        ButterKnife.bind(this);
        initComponent();
    }

    public void initComponent() {

        loadingdialog = new Dialog(this);
        loadingdialog.setContentView(R.layout.dialog_loading);
        loadingdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        loadingdialog.setCanceledOnTouchOutside(false);

    }

    @OnClick(value = { R.id.backbt, R.id.modifybt})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;

            case R.id.modifybt:
                   if(TextUtils.isEmpty(editPassword.getText().toString().trim())){
                       Toast.makeText(getApplicationContext(),"支付密码不能为空",Toast.LENGTH_SHORT).show();
                        return;
                   }
                if(TextUtils.isEmpty(editPasswordconfirm.getText().toString().trim())){
                    Toast.makeText(getApplicationContext(),"确认支付密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!TextUtils.equals(editPassword.getText().toString().trim(),editPasswordconfirm.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "密码和确认密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (editPassword.getText().toString().trim().length()<6) {
                    Toast.makeText(getApplicationContext(), "支付密码不能太简短", Toast.LENGTH_SHORT).show();
                    return;
                }

                modfiyPayPassword(editPassword.getText().toString().trim());
                break;
        }
    }



    public void webUserServer() {
        final List<User> userList = Xutils.queryAll(getApplication(), User.class);
        if (userList == null || userList.size() == 0) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("userId", userList.get(0).getUserId() + "");
        Xutils.POST(HttpUrl.queryuserbyid, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                UserDomain userDomain = gson.fromJson(result, UserDomain.class);
                switch (userDomain.getState()) {
                    case HttpCode.SUCCESS:
                        User user = userDomain.getData();
                        user.setId(userList.get(0).getId());
                        Xutils.update(getApplication(), user);
                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    //设置支付密码
    public void modfiyPayPassword(String userPaypassword){
         List<User> userList= Xutils.queryAll(getApplication(),User.class);
            loadingdialog.show();
            Map<String,String> map=new HashMap<>();
               map.put("userId",""+userList.get(0).getUserId());
               map.put("userPaypassword",userPaypassword);
            Xutils.POST(HttpUrl.usermodifypaypassword, map, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    loadingdialog.dismiss();
                    try {
                        JSONObject jb=new JSONObject(result);
                        String message=jb.getString("message");
                        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
                        switch (jb.getInt("state")){
                            case HttpCode.SUCCESS:
                                webUserServer();
                                finish();
                                break;
                            case HttpCode.ERROR:
                                break;
                            case HttpCode.THROW:
                                break;
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    ex.printStackTrace();
                    loadingdialog.dismiss();

                    Toast.makeText(getApplicationContext(),"网络有误",Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });



        }



    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
